package Servlet.Manager;

import Service.UserByPageService;
import Table.PageBean;
import Table.User;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/managerUserByPageServlet")
public class ManagerUserByPageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        String totalPage = req.getParameter("totalPage");
        //从登录页面进来时，俩个参数为空，做判断 当点击下一页小于等于0时，表示显示第一页
        if (currentPage == null || Integer.parseInt(currentPage) <= 0) {
            currentPage = "1";//当前页码
        }
        //判断最后一页，不进行翻页
        if (totalPage != null && Integer.parseInt(currentPage) >= Integer.parseInt(totalPage)) {
            currentPage = totalPage;
        }

        if (rows == null) {
            rows = "4";//每页有几条数据
        }

        //调用service查询
        PageBean<User> pageBean = UserByPageService.FindUser(currentPage, rows);
        String s = JSON.toJSONString(pageBean);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println(s);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
